//
//  YDBBannerFlowLayout.swift
//  BaseFramework
//
//  Created by 杨东宝 on 2020/5/9.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit

class YDBBannerFlowLayout: UICollectionViewFlowLayout {
    
    var param:YDBBannerParam!
    override init() {
        super.init()
    }
    convenience init(param:YDBBannerParam) {
        self.init()
        self.param = param
    }
    
    override func prepare() {
        
          super.prepare()
        self.itemSize = self.param!.wItemSize ?? CGSize(width: self.collectionView!.frame.size.width/2, height:  self.collectionView!.frame.size.height)
        self.minimumInteritemSpacing = (self.param!.wFrame!.size.height - self.itemSize.height)/2
        self.minimumLineSpacing = self.param!.wLineSpacing
        self.sectionInset = self.param!.wSectionInset;
        if self.collectionView!.isPagingEnabled {
            self.scrollDirection = self.param!.wVertical ? .vertical : .horizontal
        }else{
            self.scrollDirection = .horizontal
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        self.cardScaleTypeInRect(rect: rect)
        
    }
    
    //卡片缩放
    func cardScaleTypeInRect(rect:CGRect) -> [UICollectionViewLayoutAttributes] {
        
        let array = self.getCopyOfAttributes(attributes: super.layoutAttributesForElements(in: rect)!)
        
        if !self.param!.wScale || self.param.wMarquee {
            return array
        }
        
        var visibleRect = CGRect(x: 0, y: 0, width: 0, height: 0)
        visibleRect.origin = self.collectionView!.contentOffset
        visibleRect.size = self.collectionView!.bounds.size
        
        for (_,attributes) in array.enumerated() {
   
            let distance:CGFloat = visibleRect.midX - attributes.center.x;
            let normalizedDistance :CGFloat = CGFloat(fabsf(Float(distance) / self.param!.wActiveDistance))
            let zoom:CGFloat = 1 - CGFloat(self.param!.wScaleFactor)  * normalizedDistance

            attributes.transform3D = CATransform3DMakeScale(1.0, zoom, 1.0);
            attributes.frame = CGRect(x:attributes.frame.origin.x,y: attributes.frame.origin.y + zoom, width:attributes.size.width,height: attributes.size.height);
            
            if (self.param!.wAlpha<1) {
                let collectionCenter:CGFloat =  self.collectionView!.frame.size.width / 2 ;
                let offset:CGFloat  = self.collectionView!.contentOffset.x ;
                let normalizedCenter: CGFloat  =  attributes.center.x - offset;
                let maxDistance: CGFloat  = (self.itemSize.width) + self.minimumLineSpacing;
                let distance1: CGFloat  = CGFloat(min((fabsf(Float(collectionCenter - normalizedCenter))), Float(maxDistance)))
   
                let ratio: CGFloat  = (maxDistance - distance1) / maxDistance;
                let alpha:CGFloat  = ratio * (1 - CGFloat(self.param!.wAlpha) + CGFloat(self.param!.wAlpha));
                attributes.alpha = alpha;
                
            }
            if (self.param!.wZindex) {
                
                attributes.zIndex = Int(zoom*100);
            }
            
            attributes.center = CGPoint(x:attributes.center.x, y:(self.param!.wPosition == .BannerCellPositionBottom ? attributes.center.y:self.collectionView!.center.y) + zoom);
     
        }
        
        return array
    }
    
    func getCopyOfAttributes(attributes:Array<UICollectionViewLayoutAttributes>) -> Array<UICollectionViewLayoutAttributes> {
        
        var copyArr = Array<UICollectionViewLayoutAttributes>()
        
        for attr in attributes {
            copyArr.append(attr)
        }
        return copyArr;
        
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        if self.param!.wMarquee {
            return false
        }
        return !self.collectionView!.isPagingEnabled
    }
    
    
    
   // collectionView停止滚动时的偏移量
    
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        
        if self.collectionView!.isPagingEnabled || self.param!.wMarquee {
               return proposedContentOffset;
           }
           var rect:CGRect = CGRect()
           rect.origin.y = 0;
           rect.origin.x = proposedContentOffset.x;
           rect.size = self.collectionView!.frame.size;
        let array:Array<UICollectionViewLayoutAttributes> = super.layoutAttributesForElements(in: rect)!
         
         
           
        let centerX: CGFloat  = proposedContentOffset.x + self.collectionView!.frame.size.width * self.param!.wContentOffsetX;
        
           var minDelta:CGFloat  = CGFloat(MAXFLOAT);
           for attrs in array {
            
             if Swift.abs(minDelta) >  Swift.abs(attrs.center.x - centerX){
                 minDelta = attrs.center.x - centerX;
             }
           }

         var proposedContent:CGPoint = CGPoint()
        
           proposedContent.x = proposedContentOffset.x + minDelta;
           proposedContent.y = proposedContentOffset.y
           if (!self.param.wCardOverLap) {
            self.param!.myCurrentPath = Int(roundf(Float(abs(proposedContentOffset.x) - (self.itemSize.width+self.param!.wLineSpacing))))
            }

           return proposedContent;
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
